package com.zr.subClass.service.impl;

import com.zr.planClass.model.PlanClassAllVo;
import com.zr.planClass.model.PlanClassVo;
import com.zr.planClass.model.PlanStutas;
import com.zr.subClass.mapper.SubClassMapper;
import com.zr.subClass.model.PageSelectVo;
import com.zr.subClass.model.SubClassAllVo;
import com.zr.subClass.model.SubClassVo;
import com.zr.subClass.model.SubStutas;
import com.zr.subClass.service.SubClassService;
import com.zr.util.AllRecords;
import com.zr.util.ErrorMessageClass;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by 天赐 on 2019/12/20.
 */
@Service
@Transactional
public class SubClassServiceImpl implements SubClassService{

    @Autowired
    private SubClassMapper subClassMapper;

    @Override
    public ResultVo add(@Valid SubClassVo subClassVo) {

        /*参数中添加session,可以得到登录人的姓名,可以给修改人进行赋值 ses.getAtt.....*/
        ResultVo yanzheng = yanzheng(subClassVo);

        if (!yanzheng.getSuccess()){
            return yanzheng;
        }

        SubClassAllVo subClassAllVo = new SubClassAllVo();
        BeanUtils.copyProperties(subClassVo,subClassAllVo);

        /*Date nowDate = new Date();*/
        subClassAllVo.setCreateName("张三");
        subClassAllVo.setCreateTime(subClassVo.getCreateTime());
        subClassAllVo.setUpdateName("张三");
        subClassAllVo.setUpdateTime(subClassVo.getUpdateTime());

        subClassMapper.addSubClass(subClassAllVo);
        return ResultVoBuilder.success();
    }

    @Override
    public SubClassAllVo queryById(Integer id) {
        SubClassAllVo subClassAllVo = subClassMapper.queryById(id);
        return subClassAllVo;
    }

    @Override
    public ResultVo update(@Valid SubClassVo subClassVo) {

        SubClassAllVo subClassAllVo2 = subClassMapper.queryById(subClassVo.getId());
        int nameCount =  subClassMapper.queryByName(subClassVo.getName());
        if (nameCount>0){
            if (subClassAllVo2.getName().equals(subClassVo.getName())){
                subClassMapper.updateSubClass(subClassVo);
                return ResultVoBuilder.success();
            }
            return ResultVoBuilder.error(ErrorMessageClass.PLANCLASSADD);
        }

        SubClassAllVo subClassAllVo = new SubClassAllVo();
        BeanUtils.copyProperties(subClassVo,subClassAllVo);
        Date nowDate = new Date();
        subClassVo.setUpdateName("李四");
        subClassVo.setUpdateTime(nowDate);

        subClassMapper.updateSubClass(subClassVo);
        return ResultVoBuilder.success();

    }



    @Override
    @Transactional
    public ResultVo updateStudas(@Valid SubStutas subStutas) {

        /*修改的数据是否存在*/
        SubClassAllVo subClassAllVo = subClassMapper.queryById(subStutas.getId());
        if (subClassAllVo == null){
            return ResultVoBuilder.error("修改对象不存在");
        }

        /*数据转换*/
        /*修改数据库*/
        SubClassVo subClassVo = new SubClassVo();
        BeanUtils.copyProperties(subStutas,subClassVo);

        Date nowDate = new Date();

        subClassVo.setUpdateName("张三");
        subClassVo.setUpdateTime(nowDate);

        subClassMapper.updateStudas(subStutas);
        return ResultVoBuilder.success();
    }

    @Override
    public ResultVo getPage(PageSelectVo pageSelectVo) {

        /*查询当前页信息*/
        List<SubClassAllVo> list = subClassMapper.getPage(pageSelectVo);
        if (!CollectionUtils.isEmpty(list)){
            for (SubClassAllVo subClassAllVo :list) {
                if (subClassAllVo.getStatus()!=null){
                    if (!subClassAllVo.getStatus()){
                        subClassAllVo.setStatusName("禁用");
                    }
                    if (subClassAllVo.getStatus()){
                        subClassAllVo.setStatusName("启用");
                    }
                }

            }
        }


        /*查询条数*/
        Integer countTotal = subClassMapper.getCount(pageSelectVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(list);
        allRecords.setTotalNumber(countTotal);
        allRecords.setPageIndex(pageSelectVo.getPageIndex());
        allRecords.setPageSize(pageSelectVo.getPageSize());
        allRecords.resetTotalNumber(countTotal);
        return ResultVoBuilder.success(allRecords);
    }




    public ResultVo yanzheng( SubClassVo subClassVo){
        //根据名称传递数据,查看是否存在数据
        int nameCount =  subClassMapper.queryByName(subClassVo.getName());
        if (nameCount>0){

            return ResultVoBuilder.error(ErrorMessageClass.PLANCLASSADD);
        }


        return ResultVoBuilder.success();
    }
}
